
import 'package:flutter/material.dart';
import 'package:flutter_frame/app/core/values/colors.dart';
import 'package:flutter_frame/app/core/values/dimens.dart';
import 'package:get/get.dart';

import '../utils/function_util.dart';

/// @Author: leijingxing
/// @Date: 2023-3-17
/// @LastEditTime: 2023-3-17
/// @LastEditors: leijingxing
/// @Description: widget的扩展类,可以方便快速创建点击事件
extension WidgetExtension on Widget {
  /// 点击事件，防止快速点击
  ///
  /// [disable] 表示是否要禁用点击事件, 例如点击作者头像, 如果当前用户为作者则不响应
  /// [showHighlight] 是否显示水波纹
  Widget clickOnce(
    VoidCallback onTap, {
    bool disable = false,
    bool showHighlight = false,
    VoidCallback? onLong,
    VoidCallback? onDoubleTap,
  }) {
    if (disable) {
      return this;
    }

    Widget child = InkWell(
      highlightColor: showHighlight ? null : Colors.transparent,
      splashColor: showHighlight ? null : Colors.transparent,
      // 不设置的话，在评价的页面不知道为什么会出现灰色的边框
      hoverColor: Colors.transparent,
      focusColor: Colors.transparent,
      onTap: () {
        FunctionUtil.throttle(() {
          onTap.call();
        });
      },
      onLongPress: onLong,
      onDoubleTap: onDoubleTap,
      child: this,
    );

    if (showHighlight) {
      child = Material(
        color: Colors.transparent,
        child: child,
      );
    }

    return child;
  }

  /// 点击事件
  Widget click(
    VoidCallback onTap, {
    bool showHighlight = false,
    VoidCallback? onLong,
  }) {
    return InkWell(
      highlightColor: showHighlight ? null : Colors.transparent,
      splashColor: showHighlight ? null : Colors.transparent,
      onTap: onTap,
      onLongPress: onLong,
      child: this,
    );
  }

  /// 显示 BottomSheetDialog
  Future<T?> showBottomSheetDialog<T>() async {
    return Get.bottomSheet<T>(
      this,
      backgroundColor: Colors.black.withOpacity(0.45),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(
          top: Radius.circular(AppDimens.dp10),
        ),
      ),
    );
  }

  /// 将整个Widget灰度化
  Widget gray({bool gray = true}) {
    if (!gray) {
      return this;
    }
    return ColorFiltered(
      colorFilter: AppColorFilters.gray,
      child: this,
    );
  }
}
